package com.bicon.botu.mall.member.api;

import com.alibaba.fastjson.JSONObject;
import com.bicon.botu.mall.member.service.IBrowsingHistory;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import com.bicon.botu.mall.member.common.MemberConstants;
import com.bicon.botu.mall.member.vo.BrowsingHistoryVo;
import com.bicon.botu.mall.member.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 李双喜
 * @Date 2017/8/22 10:29
 */
@RestController
@RequestMapping("/V1.0.0/api/BrowsingHistory")
public class BrowsingHistoryApi extends BaseController {
    @Autowired
    private IBrowsingHistory iBrowsingHistory;
    @Autowired
    private RedisHandler redisHandler;

    /**
     * 浏览历史--看相似
     * @param type          类型   商城：mall 新闻：news 医院：hospital 医生：doctor
     * @return              list
     */
    @RequestMapping(value = "/selectResemble",method = RequestMethod.POST)
    public BaseResult selectResemble(@RequestParam("type") String type){
       List<String> list = redisHandler.getGlobalRedisTemplate().getList(MemberConstants.REDIS_KEY,":" + type + ":" +getUserId());
        List<List> list1= new ArrayList<>();
        List<ProductVo> list2 = new ArrayList<>();
       for (int i =0;i<list.size();i++){
           BrowsingHistoryVo browsingHistoryVo = JSONObject.parseObject(list.get(i), BrowsingHistoryVo.class);
           //从缓存上面取数据必须要和vo或者dto里面的对应
           String product = redisHandler.getGlobalRedisTemplate().getStringValue(MemberConstants.PRODUCT_INFO,browsingHistoryVo.getId());
           if (type.equals(MemberConstants.MALL)){
              ProductVo productVo = JSONObject.parseObject(product, ProductVo.class);
              list2 = iBrowsingHistory.selectResemble(productVo.getCategoryId(),productVo.getProductPrice());
              list1.add(list2);
           }

       }
        return new BaseResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200,list1);
    }

    /**
     * 浏览历史--为你推荐
     * @param type         类型   商城：mall 新闻：news 医院：hospital 医生：doctor
     * @return
     */
    @RequestMapping(value = "/selectRecommend",method = RequestMethod.POST)
    public BaseResult selectRecommend(@RequestParam("type") String type){
        List<String > list3= new ArrayList<>();
        Map<String,List> map = new HashMap<>();
        List<ProductVo> list4 = new ArrayList();
        List<ProductVo> productVosList = new ArrayList<>();
        List<String> list = redisHandler.getGlobalRedisTemplate().getList(MemberConstants.REDIS_KEY,":" + type + ":" +getUserId());
        if (type.equals(MemberConstants.MALL)){
            int u =list.size()>=200? 200:list.size();
            for (int i =0;i< u;i++){
                BrowsingHistoryVo browsingHistoryVo = JSONObject.parseObject(list.get(i), BrowsingHistoryVo.class);
                String product = redisHandler.getGlobalRedisTemplate().getStringValue(MemberConstants.PRODUCT_INFO,browsingHistoryVo.getId());
                ProductVo productVo = JSONObject.parseObject(product, ProductVo.class);
                list4.add(productVo);
                String productCategoryId = productVo.getCategoryId();
                list3.add(productCategoryId);//类目id放入list集合
            }
            //将list3遍历，逗号隔开
            String categoryIdJson = "";
            for (String categoryId:list3){
                categoryIdJson += categoryId+",";
            }
            //List转String
            // String categoryIdJson = list.toString();
             productVosList = iBrowsingHistory.selectRecommend(categoryIdJson);
        }

        map.put("list1",list4);
        map.put("list2",productVosList);
        return sendResult200(map);
    }
}
